package wiki.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dello spostamento dei contenuti nella pagina
 * @see Setup
 */
public class SwappingServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		String current=(String) ses.getAttribute("current");
		String name=req.getParameter("name");
		String cmd=req.getParameter("cmd");
		
		if(cmd.equals("up")){
			try {
				Setup.getSetup().getMediator().geteService().contentUp(corso,current, name);
			} catch (PageNotFoundException e) {
				resp.sendRedirect("./html/errorpages/pagina404.hmtl");
				return;
			}
		}
		if(cmd.equals("down")){
			try {
				Setup.getSetup().getMediator().geteService().contentDown(corso,current, name);
			} catch (PageNotFoundException e) {
				resp.sendRedirect("./html/errorpages/pagina404.html");
				return;
			}
		}
		
		resp.sendRedirect("./view?page="+current+"&mod=mod");
	} else {
		resp.sendRedirect("./html/errorpages/nopermessi.html");
		return;
	}
		} else {
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	
	} 
	
}
